// request 网络请求工具。api 文档: https://github.com/umijs/umi-request
// 开发环境时用全路径（特别是跨域请求时）！！！
// 生产环境用相对路径即可。
import request from '@/utils/request';

import { API_PATH_PRE, VDISK_PATH_PRE, BASE_HEADERS } from '@/generic/ConfigParams';

// custom jieTan
const USR = 'usr';

export async function queryApiRule(params, disk = "pub", path = "") {
  // const vdisk = disk == 'pub' ? disk : `${USR}/${disk}`;
  const api_url = `${VDISK_PATH_PRE}${disk}${path}`;
  // let api_url = `${VDISK_PATH_PRE}${vdisk}/${path.substr(1)}`;
  console.log("=========> api_url", api_url, path)
  const response = await request(api_url, {
    headers: BASE_HEADERS,
    parseResponse: false,
  });
  console.log('=====>> response', response )
  if (!response.ok) {
    return {}
    if (response.status == 403) {
      location.href = '/user/login';
    }
    else {
      return {};
    }
    return;
  };
  const content = await response.json();
  content.items.forEach((item, i) => {
    item.key = i;
  });
  // console.log("===>> data", content.items)
  return {
    data: content.items,
    total: content.dirCount,
    success: true,
    pageSize: 10,
    current: 1
  }

}
export async function createApiRule(params) {
  const api_url = `${VDISK_PATH_PRE}${params.disk}${params.location}`;
  console.log("==>> api_url", api_url)

  return request(api_url, {
    method: 'MKCOL',
    headers: BASE_HEADERS,
  });
}
export async function updateApiRule(params) {

  const vdisk = params.disk == 'pub' ? params.disk : `${USR}/${params.disk}`;
  let api_url = `${VDISK_PATH_PRE}${params.disk}${params.srcfile}`;
  console.log("==>> api_url", api_url)
  return request(api_url, {
    method: 'MOVE',
    headers: Object.assign(BASE_HEADERS, {
      "Destination": encodeURI(`${params.disk}${params.location}`),
      "Dverwrite": (params.rw ? "T" : "F"),
    }),
    parseResponse: false,
  });
}
export async function deleteApiRule(params) {
  let api_url = `${VDISK_PATH_PRE}${params.disk}${params.location}`;
  console.log("==>> api_url", api_url)

  return request(api_url, {
    method: 'DELETE',
    headers: BASE_HEADERS,
    parseResponse: false,
  });
}
export async function downloadApiRule(params, disk = "pub", path = "") {
  // const vdisk = disk == 'pub' ? disk : `${USR}/${disk}`;
  let api_url = `${VDISK_PATH_PRE}${disk}${path}`;
  // console.log("==>> downloadApiRule", api_url)
  return await request(api_url, {
    headers: BASE_HEADERS,
    parseResponse: false,
  });
}
export async function findApiRule(params, disk = "pub", path = "") {
  const vdisk = disk == 'pub' ? disk : `${USR}/${disk}`;
  const api_url = `${VDISK_PATH_PRE}${vdisk}/${path.substr(1)}`;
  console.log("==>> words", params)
  return await request(api_url, {
    method: 'PROPFIND',
    headers: Object.assign(BASE_HEADERS, {
      "Orderby": params.order || null,
      'Codisk-name': encodeURI(params.value),
      'Deepth': params.deepth || -1,
    }),
    parseResponse: false,
  });
}

export async function shareFlie(params = {}, parse = false) {

  if (params.userid && params.path) {
    const fpath = encodeURI(params.path.substr(1));
    const vdisk = params.disk == 'pub' ? params.disk : `${USR}/${params.disk}`;
    const api_url = `${VDISK_PATH_PRE}${vdisk || 'pub'}/${fpath}`;
    console.log("==>> api_url", api_url, params)
    return await request(api_url, {
      method: 'POST',
      headers: Object.assign(BASE_HEADERS, {
        'Codisk-method': 'SHARE',
        'Codisk-touser': params.userid,
        'Codisk-convertto': params.type || "ORG",
        'Codisk-expires': params.expires || 0,
        'Codisk-unit': params.unit || 'hours',
      }),
      parseResponse: parse,
    })
  }
  else {
    return false;
  }
}
export async function shareRecords(params = {}, parse = false) {

  const api_url = `${API_PATH_PRE}share${params.fileid ? `/${params.fileid}` : ''}`;
  console.log("==>> api_url", api_url)
  return await request(api_url, {
    headers: BASE_HEADERS,
    parseResponse: parse,
  });
}

export async function queryTrashApi(params = {}, parse = false) {

  const api_url = `${API_PATH_PRE}trash${params.fileid ? `/${params.fileid}` : ''}`;
  console.log("==>> api_url", api_url)
  return await request(api_url, {
    headers: BASE_HEADERS,
    parseResponse: parse,
  });
}
export async function deleteTrashApi(params = {}, parse = false) {

  const api_url = `${API_PATH_PRE}trash${params.fileid ? `/${params.fileid}` : ''}`;
  console.log("==>> api_url", api_url)
  return await request(api_url, {
    method: 'DELETE',
    headers: BASE_HEADERS,
    parseResponse: parse,
  });
}
